package com.i2works.smartluolong.manage.controller.push;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.company.CompanyApplyDO;
import com.i2works.smartluolong.web.entity.company.CompanyApplyQuery;
import com.i2works.smartluolong.web.service.company.ICompanyApplyService;
import com.i2works.smartluolong.web.service.user.IPhp2JavaService;
import com.xiaoleilu.hutool.http.HttpRequest;
import com.xiaoleilu.hutool.http.HttpResponse;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.util.PageUtil;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/11/16 22:44
 */

@RestController
public class php2javaController {

    @Autowired
    private IPhp2JavaService php2JavaService;

    @Autowired
    private ICompanyApplyService companyApplyService;

    /**
     * 采集入口
     */
    @GetMapping(value = {"/execute"})
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result getData() {
        int totalPage = PageUtil.totalPage(900, 30);
        int[] pages = PageUtil.rainbow(1, totalPage, totalPage);
        Result result = new Result();
        try {
            for (Integer page : pages) {
                HttpRequest request = HttpUtil.createGet("http://127.0.0.1:8080/php2java/getList/" + page);
                HttpResponse response = request.execute();
                int status = response.getStatus();
                if (status == HttpStatus.SC_OK) {
                    String body = response.body();
                    List<JSONObject> objects = JSONObject.parseArray(body, JSONObject.class);
                    result = php2JavaService.php2java(objects);
                    //  return php2JavaService.php2java(objects);
                }
                System.err.println("第" + page + " 到 " + (page * 30) + "条处理完成....");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
    /**
     * 采集入口
     */
    @GetMapping(value = {"/contant"})
    @ResponseBody
    public List<String> contant() throws ServiceException {
        List<String> strings = Lists.newArrayList();

        List<CompanyApplyDO> list = companyApplyService.queryList(new CompanyApplyQuery());
        List<String> objects = Lists.newArrayList();
        for (CompanyApplyDO applyDO : list) {
            if (!strings.contains(applyDO.getCompanyBrand())) {
                objects.add(applyDO.getCompanyBrand());
            }
        }
        return objects;
    }

}
